﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace QuickTweet.Common.Services
{
    public static class ServiceLocator
    {
        private static Dictionary<string, Type> views;
        private static Dictionary<string, Action<string, Action<bool>>> confirmFunctions;
        private static Dictionary<string, Action<string>> alerts;

        static ServiceLocator()
        {
            views = new Dictionary<string, Type>(StringComparer.InvariantCultureIgnoreCase);
            confirmFunctions = new Dictionary<string, Action<string, Action<bool>>>(StringComparer.InvariantCultureIgnoreCase);
            alerts = new Dictionary<string, Action<string>>(StringComparer.InvariantCultureIgnoreCase);
        }

        #region Alterts

        public static void RegisterAlert(Enum enumValue, Action<string> action)
        {
            RegisterAlert(enumValue.ToString(), action);
        }

        public static void RegisterAlert(string alertName, Action<string> action)
        {
            alerts[alertName] = action;
        }

        public static void ShowAlert(Enum enumValue, string message)
        {
            ShowAlert(enumValue.ToString(), message);
        }

        public static void ShowAlert(string alertName, string message)
        {
            Action<string> action = null;
            if (alerts.TryGetValue(alertName, out action))
                action(message);
        }

        #endregion

        #region Confirm Functions

        public static void RegisterConfirmFunction(Enum enumValue, Action<string, Action<bool>> action)
        {
            RegisterConfirmFunction(enumValue.ToString(), action);
        }

        public static void RegisterConfirmFunction(string functionName, Action<string, Action<bool>> action)
        {
            confirmFunctions[functionName] = action;
        }

        public static void Confirm(Enum enumValue, string message, Action<bool> action)
        {
            Confirm(enumValue.ToString(), message, action);
        }

        public static void Confirm(string functionName, string message, Action<bool> action)
        {
            Action<string, Action<bool>> function = null;
            if (confirmFunctions.TryGetValue(functionName, out function))
                function(message, action);
        }

        #endregion

        #region Views

        public static void RegisterView(Enum enumValue, Type view)
        {
            RegisterView(enumValue.ToString(), view);
        }

        public static void RegisterView(string viewName, Type view)
        {
            views[viewName] = view;
        }

        public static IModalWindow GetView(Enum enumValue, params object[] args)
        {
            return GetView(enumValue.ToString(), args);
        }

        public static IModalWindow GetView(string viewName, params object[] args)
        {
            Type type = null;
            if (views.TryGetValue(viewName, out type))
            { 
                //Istanzia il tipo specificato.
                IModalWindow window = null;
                try
                {
                    window = Activator.CreateInstance(type, args) as IModalWindow;
                }
                catch (MissingMethodException)
                {
                    //Non è presente un costuttore che si aspetta i parametri specificati in ingresso: crea
                    //l'oggetto richiamando il costruttore senza parametri.
                    window = Activator.CreateInstance(type) as IModalWindow;
                }

                return window;
            }

            return null;
        }

        #endregion
    }
}
